package com.um.jdy.platform.shop.manager;

import com.um.jdy.common.shop.entity.po.GoodsCategory;
import com.um.jdy.common.shop.entity.dto.GoodsCategoryDTO;
import com.um.jdy.common.shop.entity.vo.GoodsCategoryVO;
import com.um.jdy.platform.shop.entity.dto.GoodsCategoryListDTO;
import com.um.jdy.platform.shop.entity.dto.GoodsCategoryPageDTO;
import com.um.jdy.platform.shop.entity.vo.GoodsCategoryTreeNode;
import com.um.jdy.platform.shop.service.GoodsCategoryTreeService;
import com.um.springboot.starter.orm.entity.Response;
import com.um.springboot.starter.orm.manager.BaseTreePlusManager;
import com.um.springboot.starter.utils.UMBeanUtils;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * 管理类：商品分类表(平台类目)
 */
@Component
public class GoodsCategoryManager extends BaseTreePlusManager<GoodsCategoryTreeService, GoodsCategory, GoodsCategoryDTO, GoodsCategoryVO, GoodsCategoryPageDTO, GoodsCategoryListDTO, GoodsCategoryTreeNode> {


    /**
     * 查询树结构
     * @param parentId
     * @return
     */
    @Override
    public List<GoodsCategoryTreeNode> getTree(String parentId){
        return service.getTree(parentId);
    }


    @Override
    public Response<GoodsCategory> addEntity(GoodsCategoryDTO dto) {
        GoodsCategory parent = service.getRow(dto.getShopGoodsCategoryParentId());
        String code = service.getMaxCode("code","parent_code",parent.getCode(),4);

        GoodsCategory entity = UMBeanUtils.copyBean(dto,GoodsCategory.class);
        entity.setParentCode(parent.getCode());
        entity.setCode(code);
        return Response.success(service.addEntity(entity));
    }
}
